package com.liu.sx.util;

/**
 * 用于十进制数转换成十六进制数
 *
 *
 */
public class IntToHex_Alter {

    /**
     *  INT 转 HEX
     *
     * @param n
     * @return STRING
     */
    public static String intToHex(int n) {
        StringBuffer s = new StringBuffer();
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            s = s.append(b[n%16]);
            n = n/16;
        }
        a = s.reverse().toString();
        return a;
    }

    public static int stringHexToInt(String hex){

        return  Integer.parseInt(hex,16);
    }

    public static  int byteToInt(byte n){
        if(n<0){
            return n & 0xFF;
        }else {
            return n;
        }
    }





    public static void main(String[] args) {
        System.out.println("0x16 转换成 十进制为：" +stringHexToInt("16"));
        System.out.println("213  转换成 十六进制"+intToHex(213));

        System.out.println("byte 10 转换成 int "+byteToInt((byte) 10));
        System.out.println("byte -5 转换成 int "+byteToInt((byte) -5));
        System.out.println((byte)0xd5);

    }

}
